#pragma once
#include<iostream>
#include<unordered_map>
#include<functional>
#include<ctime>
#include<unistd.h>
#include<sys/types.h>
using task_t = std::function<void()>;

static int number = 0;

//任务
void Download()
{
    std::cout << "下载任务...,id:" << getpid() << std::endl;
}
void log() 
{ 
    std::cout << "日志任务...,id:" << getpid() << std::endl; 
}
void Print() 
{ 
    std::cout << "打印任务...,id:" << getpid() << std::endl; 
}
void Sql() 
{ 
    std::cout << "数据同步任务...,id:" << getpid() << std::endl; 
}
class TaskManger
{
    public:
    TaskManger()
    {
        srand(time(nullptr));
        InsertTask(Download);
        InsertTask(log);
        InsertTask(Print);
        InsertTask(Sql);
    }
    void InsertTask(task_t t)
    {
        //插入任务
        tasks[number++] = t;
    }
    void Excute(int number)
    {
        if(tasks.find(number) == tasks.end()) return;
        //任务存在执行
        tasks[number]();
    }   
    //选择任务
    int SelectTask()
    {
        return rand() % number;
    }
    ~TaskManger()
    {

    }
    private:
    std::unordered_map<int,task_t>tasks;

};
//全局任务对象
TaskManger tm;

//纠正错误执行命令，程序加个数
void Usage(std::string proc) {
  std::cout << "Usage" << proc << "process-num" << std::endl;
}

//////////////////////////////////////////////////////////////////////////////////////////////



//子进程功能
void Worker() {
  while (true) {
    int cmd = 0;
    int n = ::read(0, &cmd, sizeof(cmd));
    if (n == sizeof(cmd)) {
      tm.Excute(cmd);
    } else if (n == 0) {
      std::cout << "id:" << getpid() << "quit..." << std::endl;
      break;
    }
  }
}